package com.cxl.demo.controller;

import com.cxl.demo.service.UserService;
import com.cxl.demo.util.JWTUtil;
import com.cxl.demo.util.TimeUpd;
import com.cxl.demo.vo.ReturnData;
import com.cxl.demo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    ReturnData returnData;
    @Autowired
    UserService userService;
    @PostMapping("/login")
    public ReturnData login(@RequestBody Map<String,Object> map){

        returnData.setData(null);
        System.out.println(map);
        User user1=userService.Login((Integer) map.get("userName"),""+map.get("passWord"));
        if(user1!=null){
            user1.setPassword("");
            user1.setToken(new JWTUtil().getToken(user1.getUserId()));
            returnData.setMsg("登陆成功");
            returnData.setStatus(true);
            returnData.setData(user1);
        }else{
            returnData.setMsg("账号或密码错误");
            returnData.setStatus(false);
        }
        return returnData;
    }
    @PostMapping("/register")
    public ReturnData register(@RequestBody User user){
        returnData.setData(null);
        user.setTypeId(1);
     //   Date date= new Date();
        String timestamp = new TimeUpd().getDate();
        user.setCreateTime(timestamp);
        Integer userId=userService.register(user);
            returnData.setMsg("注册成功");
            returnData.setStatus(true);
            returnData.setData(userId);
        return returnData;
    }

    @PostMapping("/addEnterprise")
    public ReturnData addEnterprise(@RequestBody User user){
        returnData.setData(null);
        user.setTypeId(2);
        Integer userId=userService.register(user);
       // Date date= new Date();
        String timestamp =new TimeUpd().getDate();
        user.setCreateTime(timestamp);
        returnData.setMsg("注册成功");
        returnData.setStatus(true);
        returnData.setData(userId);
        return returnData;
    }
    @PostMapping("/UpdateUser")
    public ReturnData UpdateUser(@RequestBody User user){
        returnData.setData(null);


        userService.UpdateUser(user);
        returnData.setMsg("修改成功");
        returnData.setStatus(true);
        returnData.setData("");
        return returnData;
    }
    @PostMapping("/GetUSer")
    public ReturnData GetUSer(@RequestBody Map<String,Object> map){
        returnData.setData(null);

        returnData.setMsg("查询成功");
        returnData.setStatus(true);
        returnData.setData(userService.GetUser(Integer.parseInt((String) map.get("userId"))));
        return returnData;
    }




    @PostMapping("/GetBusinessList")
    public ReturnData GetBusinessList(@RequestBody Map<String,Object> map){
        returnData.setData(null);
        returnData.setMsg("查询成功");

        map.put("page",((Integer)map.get("currentPage")-1)*(Integer)map.get("pageSize"));
        map.put("size",(Integer)map.get("pageSize"));
        returnData.setStatus(true);
        returnData.setData(userService.getBusiness(map));
        return returnData;
    }



    @PostMapping("/DelUser")
    public ReturnData DelUser(@RequestBody Map<String,Object> map){
        returnData.setData(null);

        Integer ok=userService.DelUser((Integer) map.get("userId"));

        if(ok==1){
            returnData.setMsg("删除成功");
            returnData.setStatus(true);
        }else{
            returnData.setMsg("删除失败，存在关联");
            returnData.setStatus(false);
        }



        returnData.setStatus(true);

        return returnData;
    }

}
